package com.foreveross.taskservice.core.domain.task.assign.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.taskservice.core.domain.task.TaskQueueRecordEntity;
import com.foreveross.taskservice.core.domain.task.TaskTemplateAssignDayEntity;
import com.foreveross.taskservice.core.domain.task.TaskTemplateEntity;
import com.foreveross.taskservice.core.domain.task.assign.AssignBuildQueue;
import com.foreveross.taskservice.infra.util.DateUtil;

@Component("betweenAssignBuildQueue")
public class BetweenBuildQueueBean implements AssignBuildQueue {

	@Override
	public List<TaskQueueRecordEntity> build(TaskTemplateAssignDayEntity ta) {
		List<TaskQueueRecordEntity> list = new ArrayList<TaskQueueRecordEntity>();
		if(ta.getStartDate() != null && ta.getEndDate() != null
				&& ta.getStartDate().before(ta.getEndDate())){//如果参数为空，或者说日期配置不正确
			if(ta.getEndDate().before(new Date())) return list; //当前时间已超过配置时间区间
			Date endDate = DateUtil.addDay(ta.getEndDate(), 1);
			TaskTemplateEntity template = ta.getTemplate();
			Date date = ta.getStartDate().before(new Date()) ? new Date() : ta.getStartDate();
			while(date.before(endDate)){
				list.add(new TaskQueueRecordEntity(
						template.getFromCity(), template.getFromCityName(), template.getToCity(), template.getToCityName(),
						DateUtil.formatDay(date, "yyyy-MM-dd"), 
						DateUtil.getDateAfter(date,ta.getAssignRoundDay()),
						template.getIsInternational()==null?0:template.getIsInternational().intValue(), template.getIsReturn()==null?0:template.getIsReturn(), 
										template.getGrabChannelId(), template.getGrabChannel(), template.getAreaCode(), template.getAreaName(), template.getAttachHbaseKey(), template.getPriority()));
				date = DateUtil.addDay(date, 1);
			}
		}
		return list;
	}

}
